<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户管理</title>
    <!--引入 element-ui 的样式，-->
    <link rel="stylesheet" href="../../../assets/css/elementui.css">
    <link rel="stylesheet" type="text/css" href="/assets/libs/kindeditor/themes/default/default.css"/>
</head>
<body>
<!-- 正文开始 -->
<div id="app">
    <el-form :model="robotForm" label-width="8em" :rules="rules" ref="ruleForm">
        <el-form-item label="公告标题" prop="title">
            <el-input v-model="robotForm.title"></el-input>
        </el-form-item>
        <el-form-item label="立即发布" prop="sendTime">
            <el-radio v-model="robotForm.immediate" label="1">是</el-radio>
            <el-radio v-model="robotForm.immediate" label="2">否</el-radio>
            <el-date-picker
                    v-if="robotForm.immediate==2"
                    v-model="robotForm.sendTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="请选择发送时间">
            </el-date-picker>

            </el-select>
        </el-form-item>
        <el-form-item label="使用机器人" prop="apiAuth">
            <el-select v-model="robotForm.apiAuth">
                <el-option v-for="(item,i) in apiList"
                           :label="item.menu_name"
                           :value="item.menu_id" :key="i">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
            <textarea type="textarea" name="reason" v-mdel="robotForm.content"></textarea>
        </el-form-item>
        <el-form-item label="图片" prop="content">
            <el-upload
                    class="upload-demo"
                    :action=uploadUrl
                    :on-success="uploadSuccess"
                    :on-change="changeFile"
                    :before-upload="beforeUpload"
                    :hearer="hearer">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传图片，且不超过2M</div>
            </el-upload>
        </el-form-item>
        <!--<el-form-item label="立即执行" prop="immediate">-->
        <!--<el-radio v-model="robotForm.immediate" label="1">是</el-radio>-->
        <!--<el-radio v-model="robotForm.immediate" label="2">否</el-radio>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="状态" prop="status">-->
        <!--<el-radio v-model="robotForm.status" label="1">启用</el-radio>-->
        <!--<el-radio v-model="robotForm.status" label="2">禁用</el-radio>-->
        <!--</el-form-item>-->
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveInfo">确 定</el-button>
        <el-button @click="create = false">取 消</el-button>
    </div>
</div>

<script type="text/javascript" src="../../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../../assets/js/common.js?v=317"></script>
<script type="text/javascript" src="../../../assets/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../../../assets/js/js.cookie.min.js"></script>
<script type="text/javascript" src="../../../assets/js/sqjx.js"></script>
<script type="text/javascript" src="../../../assets/js/custom.js"></script>
<script charset="utf-8" src="../../../assets/libs/kindeditor/kindeditor.js"></script>
<script charset="utf-8" src="../../../assets/libs/kindeditor/plugins/code/prettify.js"></script>
<script src="../../../assets/js/vue.min.js"></script>
<script src="../../../assets/js/elementui.js"></script>
<script>
    var editor;
    KindEditor.ready(function (K) {
        editor = K.create('textarea[name="reason"]', {
            resizeType: 1,
            allowFileManager: true,
            items: [],
            afterCreate: function () {
                var autoheight = K.IE ? this.edit.doc.body.scrollHeight : this.edit.doc.body.offsetHeight;  //判断是否是IE，并获取内容高度。
                this.edit.setHeight(autoheight);   //设置高度
                this.sync();
            },
            afterBlur: function () {
                this.sync();
            },
            afterChange: function () {  //输入文字事件
                var autoheight = K.IE ? this.edit.doc.body.scrollHeight : this.edit.doc.body.offsetHeight;  //判断是否是IE，并获取内容高度。
                this.edit.setHeight(autoheight);   //设置高度
            }
        });
        editor.sync();
        prettyPrint();
    });
</script>
<script>
    let that;
    new Vue({
        el: '#app',
        data: function () {
            return {
                uploadUrl: uploadUrl,
                hearer: {
                    token: Cookies.get('token')
                },
                robotForm: {
                    id: '',
                    title: '',
                    sendTime: '',
                    noticeType: '',
                    apiAuth: '',
                    content: '',
                    executeType: '',
                    status: '1',
                    immediate: "1",
                    image: '',
                    base64: '',
                    sendTime:'',
                },
                rules: {
                    title: [
                        {required: true, message: '请输入', trigger: 'blur'},
                        {max: 50, message: '标题长度不得大于50', trigger: 'blur'}
                    ],
                    apiAuth: [
                        {required: true, message: '请选择接口', trigger: 'blur'}
                    ],
                },

                apiList: [],
                noticeTypeList: noticeTypeList,
            }
        },
        mounted: function () {
            that = this
            that.queryUserApiPermission()
        },
        methods: {
            beforeUpload(file) {
                if (!(file.size <= 1024 * 1024 * 2)) {
                    showAlert('上传文件超过2M,请重新选择')
                    return false;
                }
            },
            changeFile(file) {
                this.getBase64(file.raw).then(res => {
                    const params = res.split(',')
                    if (params.length > 0) {
                        that.robotForm.base64 = params[1]
                    }
                })
            },
            getBase64(file) {
                return new Promise(function (resolve, reject) {
                    const reader = new FileReader()
                    let imgResult = ''
                    reader.readAsDataURL(file)
                    reader.onload = function () {
                        imgResult = reader.result
                    }
                    reader.onerror = function (error) {
                        reject(error)
                    }
                    reader.onloadend = function () {
                        resolve(imgResult)
                    }
                })
            },
            queryUserApiPermission() {
                $.get('/office/menu/queryUserApiPermission', function (res) {
                    that.apiList = res.obj;
                    var urlparams = decodeURI(getUrlParam());
                    that.robotForm = JSON.parse(urlparams);
                    if (!that.robotForm.immediate) {
                        that.robotForm.immediate = 1
                        that.robotForm.status = 1
                    }
                    editor.html(that.robotForm.content.replace(/\n\t/g, ''));
                    $("[name='reason']").val(that.robotForm.content)
                })
            },
            uploadSuccess(res) {
                that.robotForm.image = res.obj.storeName
            },
            saveInfo() {
                that.robotForm.content = $("[name='reason']").val()
                this.$refs.ruleForm.validate(validate => {
                    if (!validate) {
                        return
                    }
                    if (!that.robotForm.image && !that.robotForm.content) {
                        showAlert('图片和内容不能全为空！')
                        return
                    }
                    if (that.robotForm.immediate == 2 && !that.robotForm.sendTime) {
                        showAlert('非立即发送需要选择发送时间')
                        return
                    }

                    if (that.robotForm.id) {
                        $.post('/office/noticeManage/updateNoticeManage', this.robotForm, function (res) {
                            showAlert(res.msg, 1,function() {
                                parent.location.reload()

                            })
                        })
                    } else {
                        $.post('/office/noticeManage/addNoticeManage', this.robotForm, function (res) {
                            showAlert(res.msg, 1,function() {
                                parent.location.reload()

                            })
                        })
                    }
                })
            },
            handleEdit(row) {
                that.create = true
                that.isEdit = true
                that.robotForm = row;
            },
            handleDelete(id) {
                layer.confirm('确认删除？', function () {
                    $.post('/office/noticeManage/deleteNoticeManage', {id}, function (res) {
                        that.search()
                        showAlert(res.msg, 1)
                    })
                })
            },
            handleSizeChange() {
            },
            handleCurrentChange(currentPage) {
                that.page.currentPage = currentPage;
                that.search()
            },
        },
    })
</script>
</body>
</html>
